import { defineStore } from "pinia";
import { FondsState } from "@/stores/interface";
import { FONDS_DATA } from "../mutation-types";
import piniaPersistConfig from "@/config/piniaPersist";
import vueAxios from "@/api/index";
import ACrypto from "@/utils/crypto";

export const useFondsStore = defineStore({
  id: "fonds",
  state: (): FondsState => ({
    fondsObj: null, // 全宗点击对象
    fondsData: [] // 全宗数据
  }),
  getters: {
    getFondsData(state) {
      if (state.fondsData.length === 0) {
        const newFondsData = ACrypto.getSS(window.sessionStorage.getItem(FONDS_DATA) as string, FONDS_DATA);
        state.fondsData = newFondsData || [];
      }
      return state.fondsData;
    }
  },
  actions: {
    // 修改FondsObj
    SetFondsObj(data: any) {
      this.fondsObj = data;
    },
    async setFondsData() {
      await vueAxios.get("MasterPermission/GetUserPermissionFondsFM", {}, { noLoading: true }).then((res: any) => {
        this.fondsData = res.List;
        ACrypto.setSS(this.fondsData, FONDS_DATA);
      });
    }
  },
  persist: piniaPersistConfig("fonds", ["fondsObj"])
});
